﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HIDB;
using Model;
using Common;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class SysOperationLog : DB
    {
        public SysOperationLog(LoginInfo info)
        {
            base.loginInfo = info;
        }
        public bool InitLogin()
        {
            try
            {
                HIDB.dbo con = HIDB.common.make();
                DataTable dt = con.GetDataTable("SELECT count(1) as [Count]  FROM [Working_db].[Sys_Operation_Log]");
                if (Convert.ToInt32(dt.Rows[0][0]) > 0)
                    return false;
                else
                    return true;
            }
            catch (Exception ex) { throw ex; }
        }

        public void Log(string ParentID, string OperationIP, string OperationAction, OperationStatus OperationStatus, string OperationResult, string OperationUser)
        {
            try
            {
                HIDB.dbo con = HIDB.common.make();
                IDataParameter[] queryParas = new SqlParameter[6];
                queryParas[0] = new SqlParameter("@ParentID", ParentID);
                queryParas[1] = new SqlParameter("@OperationIP", OperationIP);
                queryParas[2] = new SqlParameter("@OperationAction", OperationAction);
                queryParas[3] = new SqlParameter("@OperationStatus", (int)OperationStatus);
                queryParas[4] = new SqlParameter("@OperationResult", OperationResult);
                queryParas[5] = new SqlParameter("@OperationUser", OperationUser);
                con.StoredUpdate("Working_db.SYS_OperationLog", queryParas);
            }
            catch (Exception ex) { throw ex; }
        }

        public int LogETL(string TaskDetailsID, string ETL_ID, DateTime StepRunTime
           , string StepRunResult, int StepStatus, string OpreateUser)
        {
            int num = -1;

            try
            {
                string strSql = @"INSERT INTO [Working_db].[ETL_Task_Details_HistoryLog]
        ([GUID],[TaskDetailsID],[ETL_ID],[StepRunTime]
        ,[StepRunResult],[StepStatus],[OpreateUser],[CreateTime])
	VALUES(@GUID,@TaskDetailsID,@ETL_ID,@StepRunTime
        ,@StepRunResult,@StepStatus,@OpreateUser,@CreateTime)";

                HIDB.dbo con = base.conn();
                SqlParameter[] cmdParms = new SqlParameter[]
                {
                    new SqlParameter("@GUID",Guid.NewGuid())
                     ,new SqlParameter("@TaskDetailsID",TaskDetailsID)
                     ,new SqlParameter("@ETL_ID",ETL_ID)
                     ,new SqlParameter("@StepRunTime",StepRunTime)
                     ,new SqlParameter("@StepRunResult",StepRunResult)
                     ,new SqlParameter("@StepStatus",StepStatus)
                     ,new SqlParameter("@OpreateUser",OpreateUser)
                     ,new SqlParameter("@CreateTime",DateTime.Now)
                };
                num = con.ExecuteSql(strSql, cmdParms);
            }
            catch (Exception ex) { throw ex; }
            return num;

        }

    }
}
